package com.ylfstu.Poly_.polyarr_;

public class PolyArray {
    public static void main(String[] args) {
        Person[] persons = new Person[5];
        persons[0] = new Person("jack", 20);
        persons[1] = new Student("mary", 18, 100);
        persons[2] = new Student("smith", 20, 98);
        persons[3] = new Teacher("scott", 30, 20000);
        persons[4] = new Teacher("king", 30, 25000);

        //循环遍历多态数组，调用say
        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i].say());//动态绑定机制

            if (persons[i] instanceof Student) {
                Student student = (Student) persons[i];
                student.Study();
//                ((Student) persons[i]).Study();
            } else if(persons[i] instanceof Teacher) {
                Teacher teacher = (Teacher) persons[i];
                teacher.teach();
            } else if(persons[i] instanceof Person){
                //
            } else {
                System.out.println("类型有误......");
            }
        }
        System.out.println();

    }
}
